import requests
import json
from collections import Counter

# 请求头参数设置
HEADERS = {
    'Host': 'match.yuanrenxue.com',
    'Connection': 'keep-alive',
    'Content-Length': '0',
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
    'User-Agent': 'yuanrenxue.project',
    'Accept': '*/*',
    'Origin': 'http://match.yuanrenxue.com',
    'Referer': 'http://match.yuanrenxue.com/match/3',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
}
logo_url = 'http://match.yuanrenxue.com/logo'
# 通过session会话形式发送请求，维持会话，之后请求其他页面就不用重新发送请求头信息
session = requests.session()
# session层面设定headers，发送的是有序字典
session.headers = HEADERS


res_list = []

# 请求数据并打印结果
for i in range(5):
    logo_res = session.post(logo_url)
    # 打印请求头与服务器返回的cookies信息
    # print(r.request.headers)
    print(dict(logo_res.cookies))
    data_res = session.get('http://match.yuanrenxue.com/api/match/3?page=' + str(i + 1))
    page_content = data_res.text
    print(f'获取第【{i + 1}】页数据：{page_content}')
    con_obj = json.loads(page_content)
    for d in con_obj['data']:
        res_list.append(d['value'])

counter = Counter(res_list)
print('获取最终结果：', counter)

